package org.meb.speedway.model.event;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.Where;
import org.meb.speedway.model.registry.Team;

@Entity
@Table(name = "t_event")
@Getter
@Setter
@ToString(exclude = { "homeMeetingRiders", "awayMeetingRiders", "heats" })
public class Event implements Serializable {

	private static final long serialVersionUID = 7326212519867620053L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Version
	private Long version;

	@Column(nullable = false)
	private Date date;

	@ManyToOne(optional = false)
	@JoinColumn(name = "home_team_id")
	@ForeignKey(name = "EVNT_HOTE_FK")
	private Team homeTeam;

	@ManyToOne(optional = false)
	@JoinColumn(name = "away_team_id")
	@ForeignKey(name = "EVNT_AWTE_FK")
	private Team awayTeam;

	@OneToMany(mappedBy = "event")
	private List<Heat> heats;

	@OneToMany(mappedBy = "event", cascade = CascadeType.ALL)
	@Where(clause = "rider_kind = 'HOME'")
	private List<EventRider> homeEventRiders;

	@OneToMany(mappedBy = "event", cascade = CascadeType.ALL)
	@Where(clause = "rider_kind = 'AWAY'")
	private List<EventRider> awayEventRiders;

}
